//
//  WFDesignerViewController.m
//  谷居
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFDesignerViewController.h"
#import "WFLabel.h"
#import "WFButtonText.h"
#import "WFTableView.h"
#import "WFDesignerCell.h"

#import "AFNetworking.h"
#import "WFProfessionalModel.h"
#import "WFUserimageModel.h"

#import "MJRefresh.h"

#import "WFDesignerDetailVC.h"
@interface WFDesignerViewController ()
<
UITableViewDataSource,
UITableViewDelegate

>

@property (nonatomic, retain) UIImageView *hairlineImageViewUnder;


@property (nonatomic, retain) WFLabel *titleLabel;

@property (nonatomic, retain) WFButtonText *allStateButton; /** 全国按钮. */

@property (nonatomic, retain) WFTableView *designerTableView;

@property (nonatomic, retain) NSMutableArray *professiionalArray; /** 装载model. */

@property (nonatomic, retain) NSMutableArray *userImageArray; /** 用户头像数组. */

@property (nonatomic, assign) NSInteger start;

@property (nonatomic, assign) NSInteger count;

@property (nonatomic, retain) WFDesignerDetailVC *detailVC;

@end

@implementation WFDesignerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.professiionalArray = [NSMutableArray array];
    self.userImageArray = [NSMutableArray array];
    
    /** 创建导航栏. */
    [self createNavigationView];
    
    
    /** 创建tableView. */
    [self createTableView];
    
//    /** 网络请求 */
    [self refresh];
}


#pragma mark - 创建导航栏
- (void)createNavigationView {
    _hairlineImageViewUnder = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    /** 创建titleLabel. */
    [self createTitleLabel];
    
    /** 创建全国button. */
    [self createAllStateButton];
}


- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0 ) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
    
}

/** 创建titleLabel. */
- (void)createTitleLabel {
    _titleLabel = [[WFLabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    self.navigationItem.titleView = _titleLabel;
    _titleLabel.text = @"设计师";
    _titleLabel.textAlignment = 1;
}


/** 设置全国button. */
- (void)createAllStateButton {
    _allStateButton = [WFButtonText buttonWithType:UIButtonTypeCustom];
    _allStateButton.frame = CGRectMake(0, 0, 32, 32);
    [_allStateButton setTitle:@"全国" forState:UIControlStateNormal];
    _allStateButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_allStateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_allStateButton];
    
}


#pragma mark - 网络请求
- (void)makeRequestWithUrl:(NSString *)url upDown:(BOOL)upDown {
    NSLog(@"designerurl: %@", url);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *tempArray = responseObject[@"professionals"];
        [self.userImageArray removeAllObjects];
        
        if (upDown) {
            [self.professiionalArray removeAllObjects];
            self.professiionalArray = [WFProfessionalModel baseModelByArray:tempArray];
        } else {
            NSMutableArray *arr = [WFProfessionalModel baseModelByArray:tempArray];
            [self.professiionalArray addObjectsFromArray:arr];
        }
        NSLog(@"%@", self.professiionalArray);
        
        /** 创建userImageArray. */
        for (WFProfessionalModel *proModel in self.professiionalArray) {
            WFUserimageModel *userImageModel = [[WFUserimageModel alloc] init];
            [userImageModel setValuesForKeysWithDictionary:(NSDictionary *)proModel.userImage];
            [self.userImageArray addObject:userImageModel];
            [userImageModel release];
        }
        
        
        [_designerTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}

#pragma mark - 上拉刷新

- (void)refresh {
    self.start = 0;
    self.count = 10;
    NSString *str = @"%E5%85%A8%E5%9B%BD";
    NSString *url = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/professionals?start=%ld&count=%ld&user=(null)&city=%@", self.start, self.count, str];
    [self makeRequestWithUrl:url upDown:YES];
    [_designerTableView.mj_header endRefreshing];
}

- (void)loadMore {
    self.start += 10;
    NSString *str = @"%E5%85%A8%E5%9B%BD";
    NSString *url = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/professionals?start=%ld&count=%ld&user=(null)&city=%@", self.start, self.count, str];
    [self makeRequestWithUrl:url upDown:NO];
    [_designerTableView.mj_footer endRefreshing];

}






#pragma mark - 创建tableView

- (void)createTableView {
    _designerTableView = [[WFTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 ) style:UITableViewStylePlain];
    [self.view addSubview:_designerTableView];
    [_designerTableView release];
    _designerTableView.delegate = self;
    _designerTableView.dataSource = self;
    _designerTableView.rowHeight = 100;
    [_designerTableView registerClass:[WFDesignerCell class] forCellReuseIdentifier:@"designer"];
    
    _designerTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _designerTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
    
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.professiionalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WFDesignerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"designer" forIndexPath:indexPath];
    cell.professionalModel = self.professiionalArray[indexPath.row];
    cell.userImageModel = self.userImageArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    /** push vc*/
    _detailVC = [[WFDesignerDetailVC alloc] init];
    [self.navigationController pushViewController:_detailVC animated:YES];
    [_detailVC release];
    WFProfessionalModel *Model = self.professiionalArray[indexPath.row];
    NSLog(@"%@", Model.userName);
    
    _detailVC.userImageModel = self.userImageArray[indexPath.row];
    _detailVC.professionnalModel = self.professiionalArray[indexPath.row];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _hairlineImageViewUnder.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [super viewDidAppear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_professiionalArray release];
    [super dealloc];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
